{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append(\"../\")\n",
    "from initial_dataSet import DataSet\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import torch\n",
    "\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DataSet: JUNYI\n"
     ]
    }
   ],
   "source": [
    "basedir = '../'\n",
    "dataSet_list = ('JUNYI', 'MathEC')\n",
    "\n",
    "dataSet_idx=0\n",
    "data_set_name = dataSet_list[dataSet_idx]\n",
    "dataSet = DataSet(basedir, data_set_name)\n",
    "\n",
    "read_dir=dataSet.save_parameter_dir\n",
    "conc_inter=np.loadtxt(read_dir+'H.csv',delimiter=',')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "skill_data=pd.read_csv(dataSet.read_dir+'concept_relationship.csv')\\\n",
    "    .loc[:,['knowledge_code','parent']].dropna(how='any').astype('int')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 召回率\n",
    "from sklearn  import metrics\n",
    "\n",
    "def evaluate(y_pred,y_true):\n",
    "    acc=metrics.accuracy_score(y_true, y_pred)\n",
    "    recall=metrics.recall_score(y_true, y_pred,average='macro') # 召回率\n",
    "    return acc,recall"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "label=np.zeros((dataSet.concept_num,dataSet.concept_num))\n",
    "label[skill_data['parent']-1,skill_data['knowledge_code']-1]=1\n",
    "# label[skill_data['knowledge_code']-1,skill_data['parent']-1]=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp=conc_inter.copy()\n",
    "tmp[np.eye(conc_inter.shape[0],dtype='bool')]=0\n",
    "pred=(tmp-2*tmp.mean(axis=0)>0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "acc:0.9590, recall:0.9088\n"
     ]
    }
   ],
   "source": [
    "acc,recall=evaluate(pred.reshape(-1),label.reshape(-1))\n",
    "print('acc:%.4f, recall:%.4f'%(acc,recall))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.9963104102985336, 0.5)"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evaluate(np.zeros_like(label).reshape(-1),label.reshape(-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.8, 0.5)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a=np.array([0,1,1,0,0,0,0,0,0,0])\n",
    "b=np.array([0,0,0,0,0,0,0,0,0,0])\n",
    "\n",
    "evaluate(b,a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# RCD\n",
    "\n",
    "dataSet_list = ('JUNYI', 'MathEC')\n",
    "if data_set_name == 'JUNYI':\n",
    "    rcd_read_dir='../Baseline/SelfRCD/graph/junyi/计算出的concept_relationship.csv'\n",
    "elif data_set_name == 'MathEC':\n",
    "    rcd_read_dir='../Baseline/SelfRCD/graph/math_ec/concept_relationship.csv'\n",
    "rcd_conc_inter=pd.read_csv(rcd_read_dir)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "rcd_pred=np.zeros_like(label)\n",
    "rcd_pred[rcd_conc_inter['concept_1']-1,rcd_conc_inter['concept_2']-1]=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.7447373331460966, 0.7854083155640854)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evaluate(rcd_pred.reshape(-1),label.reshape(-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>knowledge_code</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>item_id</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>[1]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>[2]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>[3]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>[4]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>[5]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>717</th>\n",
       "      <td>[717]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>718</th>\n",
       "      <td>[718]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>719</th>\n",
       "      <td>[719]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>720</th>\n",
       "      <td>[720]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>721</th>\n",
       "      <td>[721]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>721 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        knowledge_code\n",
       "item_id               \n",
       "1                  [1]\n",
       "2                  [2]\n",
       "3                  [3]\n",
       "4                  [4]\n",
       "5                  [5]\n",
       "...                ...\n",
       "717              [717]\n",
       "718              [718]\n",
       "719              [719]\n",
       "720              [720]\n",
       "721              [721]\n",
       "\n",
       "[721 rows x 1 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# CDGK\n",
    "dataSet.item\n"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "527a93331b4b1a8345148922acc34427fb7591433d63b66d32040b6fbbc6d593"
  },
  "kernelspec": {
   "display_name": "Python 3.8.12 ('pytorch')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
